package cn.tedu._03vehicle.base.filter;

import cn.tedu._03vehicle.pojo.entity.User;
import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import jakarta.servlet.http.HttpServletRequest;

@WebFilter("/v1/user/*")
public class LoginCheckFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        HttpSession session = request.getSession();
        User user = (User)session.getAttribute("user");
        if (user!=null){
            filterChain.doFilter(request,response);
        }else {
            //重定向到登录页面
            response.sendRedirect("/login.html");
        }
    }
}
